package com.tanhua.locationmanager.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.SurveyQuestion;
import com.tanhua.dubbo.api.db.SurveyQuestionsApi;
import com.tanhua.vo.OptionVo;
import com.tanhua.vo.PageResult;
import com.tanhua.vo.QuestionVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 试卷问题增删改查
 *
 * @author Dzh
 * @date 2021/07/17 20:07:03
 */
@Service
public class QuestionService {

    /**
     * 调查问题的api
     */
    @Reference
    private SurveyQuestionsApi surveyQuestionsApi;

    /**
     * @param paperId  纸id
     * @param page     页面
     * @param pageSize 页面大小
     * @return {@link ResponseEntity<?> }
     * @Author Dzh
     * @date 2021/07/17 21:07:19
     */
    public ResponseEntity<?> queryByPage(Integer paperId, Integer page, Integer pageSize) {
       Page<SurveyQuestion> iPage = surveyQuestionsApi.queryQuestions(paperId,page,pageSize);
       PageResult pageResult = new PageResult((int)iPage.getCurrent(),(int)iPage.getSize(),(int)iPage.getTotal(),iPage.getRecords());
       return ResponseEntity.ok(pageResult);
    }

    /**
     * @param questionId 问题id
     * @return {@link ResponseEntity<?> }
     * @Author Dzh
     * @date 2021/07/17 21:07:28
     */
    public ResponseEntity<?> queryQuestionById(Integer questionId) {
        SurveyQuestion surveyQuestions =surveyQuestionsApi.queryQuestionsById(questionId);
        return ResponseEntity.ok(surveyQuestions);
    }

    /**
     * @param questionVo 问题
     * @return {@link ResponseEntity<?> }
     * @Author Dzh 修改  霍
     * @date 2021/07/17 21:07:24
     */
    public ResponseEntity<?> saveQuestion(QuestionVo questionVo) {
        surveyQuestionsApi.saveQuestions(questionVo);
        return ResponseEntity.ok().build();
    }

    /**
     * @param questionVo 问题
     * @return {@link ResponseEntity<?> }
     * @Author Dzh 修改 huo
     * @date 2021/07/17 21:07:34
     */
    public ResponseEntity<?> update(QuestionVo questionVo) {
        surveyQuestionsApi.update(questionVo);
        return ResponseEntity.ok().build();
    }


    /**
     * 根据问题id查询问题及对应的选项，并封装到QuestionVo中
     * @param questionId 问题id
     * @return {@link ResponseEntity<?> }
     * @Author Huo
     * @date 2021/07/18 09:07:50
     */
    public ResponseEntity<?> findOptionsByQuestionId(Integer questionId) {
        QuestionVo questionVo = surveyQuestionsApi.findOptionsByQuestionId(questionId);
        System.out.println(questionVo);
        return ResponseEntity.ok(questionVo);
    }

    public ResponseEntity<?> deleteQuestion(Integer qid) {
        surveyQuestionsApi.deleteQuestionById(qid);
        return ResponseEntity.ok().build();
    }
}
